package com.hmy.cornpestdet.service;

import com.hmy.cornpestdet.mapper.UserMapMapper;
import com.hmy.cornpestdet.pojo.CircleArea;
import com.hmy.cornpestdet.pojo.MarkerPoint;
import com.hmy.cornpestdet.pojo.PolygonArea;
import com.hmy.cornpestdet.utils.CPDConst;
import com.hmy.cornpestdet.utils.Result;
import com.hmy.cornpestdet.vo.CircleAreaVo;
import com.hmy.cornpestdet.vo.MarkerPointVo;
import com.hmy.cornpestdet.vo.PolygonAreaVo;
import com.hmy.cornpestdet.vo.ReactAreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserMapService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserMapMapper userMapMapper;
    //标记点
    public Result getAllMarkerPointByUid(){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<MarkerPoint>points = userMapMapper.getAllMarkerPointByUid(uid);
        List<MarkerPointVo>pointVos = new ArrayList<MarkerPointVo>();
        for(MarkerPoint point : points){
            MarkerPointVo markerPointVo = new MarkerPointVo(point.getPid(),point.getPosition(),point.getTitle(),point.getInfo(),point.getAddDate(),point.getIcon());
            pointVos.add(markerPointVo);
        }
        if(points == null){
            return Result.Error("获取坐标数据错误");
        }
        return Result.Success("获取坐标数据成功",pointVos);
    }
    public Result deleteMarkerPointByPid(@RequestParam String pid){
        if(userMapMapper.deleteMarkerPointByPid(pid) < 1){
            return Result.Error("删除失败");
        }
        return Result.Success("删除成功",null);
    }
    public Result addMarkerPoint(MarkerPointVo markerPointVo){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        MarkerPoint markerPoint = new MarkerPoint();
        markerPoint.setPid(markerPointVo.getPid());
        markerPoint.setUid(uid);
        markerPoint.setPosition(markerPointVo.getPosition());
        markerPoint.setTitle(markerPointVo.getTitle());
        markerPoint.setInfo(markerPointVo.getInfo());
        markerPoint.setAddDate(markerPointVo.getAddDate());
        markerPoint.setIcon(markerPointVo.getIcon());
        if(userMapMapper.addMarkerPoint(markerPoint) < 1){
            return Result.Error("插入标记点失败");
        }
        return Result.Success("插入标记点成功",null);
    }
    //圆形区域
    public Result getCircleArea(){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        CircleArea circleArea = userMapMapper.getCircleArea(uid);
        if(circleArea == null){
            return Result.Error("获取数据失败");
        }
        return Result.Success("获取数据成功",circleArea);
    }

    public Result addCircleArea( CircleAreaVo circleAreaVo){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        //如果已经存在圆形区域，执行更新操作，不存在则插入新数据
        CircleArea circleArea = userMapMapper.getCircleArea(uid);
        //根据VO创建POJO
        CircleArea newCircleArea = new CircleArea(uid,circleAreaVo.getCenter(),circleAreaVo.getRadius(),circleAreaVo.getEditable(),circleAreaVo.getFillColor(),circleAreaVo.getAddDate());
        //不存在则插入
        if(circleArea == null){
            if(userMapMapper.addCircleArea(newCircleArea) < 1){
                return Result.Error("创建失败");
            }
            return Result.Success("创建成功",null);
        }
        //已经存在则更新
        else{
            if(updateCircleArea(newCircleArea) < 1){
                return Result.Error("更新失败");
            }
            return Result.Success("更新成功",null);
        }

    }

    public Result deleteCircleArea(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        if(userMapMapper.deleteCircleArea(uid) < 1){
            return Result.Error("删除失败");
        }
        return Result.Success("删除成功",null);
    }

    public int updateCircleArea(CircleArea circleArea){
        return userMapMapper.updateCircleArea(circleArea);
    }
    //多边形区域
    public Result getPolygonArea(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        //查询操作
        PolygonArea polygonArea = userMapMapper.getPolygonArea(uid);
        if(polygonArea == null){
            return Result.Error("查询失败");
        }
        else{
            PolygonAreaVo polygonAreaVo = new PolygonAreaVo(polygonArea.fromPointsStrGetPointsList(), polygonArea.getEditable(), polygonArea.getFillColor(),polygonArea.getAddDate());
            return Result.Success("查询成功",polygonAreaVo);
        }
    }

    public Result addPolygonArea(PolygonAreaVo polygonAreaVo){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        //查看是否已经有多边形数据了
        PolygonArea polygonArea = userMapMapper.getPolygonArea(uid);
        //创建新多边形对象
        PolygonArea nPolygonArea = new PolygonArea(uid, polygonAreaVo.getPointsStr(), polygonAreaVo.getEditable(), polygonAreaVo.getFillColor(), polygonAreaVo.getAddDate());
        //没有就新建并添加到数据库
        if(polygonArea == null){
            //插入操作
            if(userMapMapper.addPolygonArea(nPolygonArea) < 1){
                return Result.Error("插入失败");
            }
            return Result.Success("插入成功",null);
        }
        //已经存在了就更新数据
        else {
            if(updatePolygonArea(nPolygonArea) < 1){
                return Result.Error("更新失败");
            }
            return Result.Success("更新成功",null);
        }
    }

    public int updatePolygonArea(PolygonArea polygonArea){
        return userMapMapper.updatePolygonArea(polygonArea);
    }

    public Result deletePolygonArea(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        if(userMapMapper.deletePolygonArea(uid) < 1){
            return Result.Error("删除失败");
        }
        return Result.Success("删除成功",null);
    }

    //更新用户信息
    public Result updateUserProvince(String province){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        if(userMapMapper.updateUserProvince(uid,province) < 1){
            return Result.Error("更新失败");
        }
        return Result.Success("更新成功",null);
    }

    public Result getUserPointWindowInfoByPid(String pid){
        String info = userMapMapper.getUserPointWindowInfoByPid(pid);
        if(info == null){
            return Result.Error("查询失败");
        }
        return Result.Success("查询成功",info);
    }

    public Result updateUserPointWindowInfoByPid(String pid,String info){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<MarkerPoint>points = userMapMapper.getAllMarkerPointByUid(uid);
        List<MarkerPointVo>pointVos = new ArrayList<MarkerPointVo>();
        for(MarkerPoint point : points){
            MarkerPointVo markerPointVo = new MarkerPointVo(point.getPid(),point.getPosition(),point.getTitle(),point.getInfo(),point.getAddDate(),point.getIcon());
            pointVos.add(markerPointVo);
        }

        if(userMapMapper.updateUserPointWindowInfoByPid(pid,info) < 1){
            return Result.Error("修改失败");
        }
        return Result.Success("修改成功",pointVos);
    }

    //获取水稻种植区形参的矩形区域
    public Result getRiceReactArea(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        ReactAreaVo reactArea = userMapMapper.getRiceReactArea(uid);
        if(reactArea == null){
            return Result.Error("查询失败");
        }
        return Result.Success("查询成功",reactArea);
    }

}
